package com.ch.web.controller;

import com.ch.web.ResponseDto.xyDelivery.SendTaskResponseDto;
import com.ch.web.entity.XyDeliveryEntity;
import com.ch.web.requestDto.xyDeliveryRequest.DeliveryAddRequest;
import com.ch.web.service.XyDeliveryService;
import com.ch.web.service.XyUserService;
import com.ch.web.utils.PageUtils;
import com.ch.web.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

/**
 * @email 1429542285@qq.com
 * @date 2022-11-19 23:58:22
 */
@RestController
@RequestMapping("xy/delivery")
public class XyDeliveryController {
    @Autowired
    private XyDeliveryService xyDeliveryService;
@Resource
private XyUserService xyUserService;
    /**
     * 管理员获取列表
     */
    @RequestMapping(value = "/adminGetList", method = RequestMethod.POST)
    public R adminGetList(@RequestBody Map<String, Object> pagingObj) {
        PageUtils page = xyDeliveryService.adminGetList(pagingObj);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public R list(@RequestBody Map<String, Object> pagingObj) {
        PageUtils page = xyDeliveryService.queryPage(pagingObj);
        return R.ok().put("page", page);
    }

    /**
     * 我的接单list列表
     */
    @RequestMapping(value = "/receiveListTask", method = RequestMethod.POST)
    public R receiveListTask(@RequestBody Map<String, Object> pagingObj, HttpServletRequest request) {
        PageUtils page = xyDeliveryService.queryReceiveListTask(pagingObj, request);
        return R.ok().put("page", page);
    }

    /**
     * 我的发单列表
     */
    @RequestMapping(value = "/sendTaskList", method = RequestMethod.POST)
    public R getSendTaskList(@RequestBody Map<String, Object> pagingObj, HttpServletRequest request) {
        PageUtils page = xyDeliveryService.querySendTaskList(pagingObj, request);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        XyDeliveryEntity xyDelivery = xyDeliveryService.getById(id);
        String userNick = xyUserService.getById(xyDelivery.getReceiveUserId()).getUserNick();
        SendTaskResponseDto dto =new SendTaskResponseDto();
        BeanUtils.copyProperties(xyDelivery,dto);
        dto.setReceiveTaskUserName(userNick);
        return R.ok().put("xyDelivery", dto);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody XyDeliveryEntity xyDelivery) {
        xyDeliveryService.save(xyDelivery);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody XyDeliveryEntity xyDelivery) {
        xyDeliveryService.updateById(xyDelivery);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        xyDeliveryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 接单
     */
    @GetMapping("/receiveTask/{id}")
    public R receiveTask(@PathVariable("id") Long id, HttpServletRequest request) {
        boolean b = xyDeliveryService.receiveTask(id, request);
        if (b) {
            return R.ok("接单成功");
        } else {
            return R.error("接单失败,该单已被他人接走了");
        }
    }

    /**
     * 发单
     */
    @PostMapping("/sendTask")
    public R sendTask(@RequestBody DeliveryAddRequest deliveryAddRequest, HttpServletRequest request) {
        Date endTime = deliveryAddRequest.getEndTime();
        if (endTime.getTime() < new Date().getTime()) {
            return R.error("截止时间不能小于当前时间");
        }
        return R.okOrError(xyDeliveryService.saveTask(deliveryAddRequest, request));
    }

    /**
     * 撤单
     */
    @PostMapping("/cancelTask")
    public R cancelTask(@RequestBody Long id) {
        return R.okOrError(xyDeliveryService.removeById(id));
    }

    /**
     * 确认收货
     */
    @PostMapping("/confirmTask")
    public R confirmTask(@RequestBody Long id) {
        XyDeliveryEntity byId = this.xyDeliveryService.getById(id);
        byId.setStatus(2);
        return R.okOrError(this.xyDeliveryService.saveOrUpdate(byId));
    }
}
